import json
from sql_con import *
from rename import change_list_by_dict,my_dic
import re
my_dic2 = {'name':"用户名","password":"密码"}
def write_json_all_data():
    """
    用于写入所有资产，方便展示
    """
    table = show_general_sheet()
    t = []
    k = []
    if len(table)>0:
        for j in range(0, len(table[0])):
            for i in table:
                if i[j] != "": t.append(i[j])
            if len(t) == 1:
                k.append(j)
            if len(t) > 1 and t[1] == "0000-00-00":
                k.append(j)
            t = []
        k = sorted(k, reverse=True)
        for i in k:
            for j in table:
                j.pop(i)
        table[0] = change_list_by_dict(table[0], my_dic)
    place2 = '.\static\json\j'
    name = "son_data"
    name_type = "{}.json".format(name)
    place2 += name_type
    json.dump(table, open(place2, 'w'), indent=4)
def write_json_pie(name,file_name):
    """
    用于写饼图所需json文件
    :param name:
    :param file_name:
    """
    table = select_and_count(name)
    for i in table:
        i["name"] = i.pop(name)
        i["value"] = i.pop("count")
    if name == "brand":
        table_new = [{"name": "联想", "value": 0}, {"name": "戴尔", "value": 0}, {"name": "闪迪", "value": 0},
                     {"name": "其他", "value": 0}]
        for i in table:
            if len(re.findall("联想", i["name"])) > 0:
                table_new[0]["value"] = table_new[0]["value"] + i["value"]
                continue
            if len(re.findall("戴尔", i["name"])) > 0 or len(re.findall("DELL", i["name"])):
                table_new[1]["value"] = table_new[1]["value"] + i["value"]
                continue
            if len(re.findall("闪迪", i["name"])) > 0:
                table_new[2]["value"] = table_new[2]["value"] + i["value"]
                continue
            if i['value'] <= 2:
                table_new[3]["value"] = table_new[3]["value"] + i["value"]
                continue
            table_new.append(i)

        table_new.sort(key=lambda x: x["value"], reverse=True)
        place2 = r'.\static\json\j'
        name_type = "{}.json".format(file_name)
        place2 += name_type
        json.dump(table, open(place2, 'w'), indent=4)
        json.dump(table_new, open(place2, 'w'), indent=4)
    else:
        place2 = r'.\static\json\j'
        name_type = "{}.json".format(file_name)
        place2 += name_type
        json.dump(table, open(place2, 'w'), indent=4)

def write_json_users():
    """
    用于写显示用户的json文件
    """
    table = show_users()
    table[0] = change_list_by_dict(table[0], my_dic2)
    place2 = '.\static\json\j'
    name = "son_users"
    name_type = "{}.json".format(name)
    place2 += name_type
    json.dump(table, open(place2, 'w'), indent=4)
def write_json(table,name):
    """
    用于根据数据，以及文件名写入json文件
    :param table:
    :param name:
    """

    place2 = '.\static\json\j'
    name_type = "{}.json".format(name)
    place2 += name_type
    json.dump(table, open(place2, 'w'), indent=4)

if __name__ == '__main__':
    write_json_all_data()
    write_json_pie('brand',file_name = "son_pie_brand")
    write_json_pie('department', file_name="son_pie_department")
    write_json_users()